#include <iostream>
#include <stdlib.h>

using namespace std;

typedef struct List
{
    int data;
    struct List* next;
}List;

List* initlist()                     //initialize of the head of list
{
    List* head=(List*)malloc(sizeof(List));
    if(head==NULL)
    {
        cout<<"creat failed exit program"<<endl;
        exit(0);
    }
    else
    {
        head->next=NULL;
        return head;
    }
}

int create_list(List *head)               //creat the list and enter the element
{
    int data;
    cout<<"enter the element which need insert"<<endl;
    cin>>data;

    List* node=initlist();
    node->data=data;

    if(head!=NULL)
    {
        List* p=head;
        while(p->next!=head)
        {
            p=p->next;
        }
        p->next=node;
        node->next=head;
        return 1;
    }
    else 
    {
        cout<<"there are no element at head of list"<<endl;
        return 0;
    }
}


List* insert_list(List* head,int pos,int data)
{
    List* node=initlist();
    List* p=head;
    List* t;
    t=p;
    node->data=data;
    if(head!=NULL)
    {
        for(int i=1;i<=pos;i++)
        {
            t=t->next;
        }
        node->next=t->next;
        t->next=node;
        return p;
    }
    return p;
}

int delete_list(List* head)
{
    if(head==NULL)
    {
        cout<<"the list is empty"<<endl;
        return 0;
    }

    List* temp=head;
    List* ptr=head->next;

    int del;
    cout<<"enter the delete element"<<endl;
    cin>>del;

    while(ptr!=head)
    {
        if(ptr->data==del)
        {
            if(ptr->next=head)
            {
                temp->next=head;
                free(ptr);
                return 1;
            }
            temp->next=ptr->next;
            free(ptr);
            return 1;
        }
        temp=temp->next;
        ptr=ptr->next;
    }
        cout<<"can't find the delete element"<<endl;
        return 0;
 }


int display(List* head)
{
    if(head!=NULL)
    {
        List*p=head;
        while(p->next!=head)
        {
            cout<<p->next->data;
            p=p->next;
        }
        cout<<endl;
        return 1;
    }
    else
    {
        cout<<"the head list is empty"<<endl;
        return 0;
    }
}

int main ()
{
    List* head=initlist();
    head->next=head;

    for(int i=1;i<5;i++)
    {
        create_list(head);
    }
    display(head);

    head=insert_list(head,1,10);
    display(head);

    delete_list(head);
    display(head);

    system("pause");
    return 0;
}



